import { Response } from "express";
import Types from "../typings";

class ResType {
  constructor() {}

  // 响应成功
  Success: (
    res: Response,
    data?: Types.Common.PlainObjType | null,
    msg?: string
  ) => Response = (res, data = null, msg = "响应成功") => {
    return res.send({
      code: 0,
      data,
      msg,
    });
  };

  // 请求参数出错
  paramsWrong: (res: Response, msg?: string) => Response = (
    res,
    msg = "请求参数出错"
  ) => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 查询数据不存在
  isNotExist: (res: Response, msg?: string) => Response = (
    res,
    msg = "查询数据不存在"
  ) => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 新增的数据已存在
  isExist: (res: Response, msg?: string) => Response = (
    res,
    msg = "新增的数据已存在"
  ) => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 文件上传失败
  UploadFail: (res: Response, err: unknown, msg?: string) => Response = (
    res,
    err,
    msg = "文件上传失败"
  ) => {
    console.log(err);
    return res.send({
      code: 500,
      data: err,
      msg,
    });
  };

  // 数据库异常
  Database: (res: Response, err: unknown, msg?: string) => Response = (
    res,
    err,
    msg = "数据库异常"
  ) => {
    console.log(err);
    return res.send({
      code: 502,
      data: err,
      msg,
    });
  };
}

export default ResType;
